﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace WawaSoft.WawaCapturer
{
  internal  class Program
    {
          void Main(string[] args)
        {
            RawSocket socket = null;

            try
            {
                socket = new RawSocket();
                socket.CreateAndBindSocket("192.168.1.100");
                if (socket.ErrorOccurred)
                {
                    Console.WriteLine("监听出错了");
                    return;
                }
                socket.KeepRunning = true;
                socket.PacketArrival += socket_PacketArrival;
                socket.Run();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw;
            }
            finally
            {
                Console.Read();
                socket.Shutdown();
            }

        }

        static void socket_PacketArrival(object sender, PacketArrivedEventArgs args)
        {
            if (args.Protocol == "TCP" && (args.OriginationPort == 80 || args.DestinationPort ==80))
            {
                Console.WriteLine(args);
                byte[] httpData = new byte[args.MessageLength - 20];
                Buffer.BlockCopy(args.MessageBuffer, 20, httpData, 0, httpData.Length);

                using (BinaryWriter bw = new BinaryWriter(File.Open(string.Format("{0}.cap",
                    DateTime.Now.ToString("yyyyMMdd hh")),
                    FileMode.OpenOrCreate)))
                {
                    bw.Write(DateTime.Now.ToString());
                    bw.Write(httpData, 0, httpData.Length);
                    bw.Write(new byte[] { 0, 0, 0, 0 }, 0, 4); //结束
                }
                return;

                //System.IO.MemoryStream s = new System.IO.MemoryStream(httpData);
                //System.IO.BinaryReader r = new System.IO.BinaryReader(s);
                //byte temp = new byte();
                //byte temp1;
                //int i;
                //bool find = false;
                //for (i = 0; i < s.Length; i++)
                //{
                //    temp1 = r.ReadByte();
                //    if (temp == 0x0d && temp1 == 0x0a)
                //    {
                //        find = true;
                //        break;
                //    }
                //    temp = temp1;
                //}
                //if (!find) return;
                //byte[] httpHeaderData = new byte[httpData.Length - i -1];
                //Buffer.BlockCopy(httpData, i, httpHeaderData, 0, httpHeaderData.Length);                

                //Console.WriteLine("http头内容如下"r"n{0}",Encoding.UTF8.GetString(httpHeaderData));

                //byte[] body = new byte[httpData.Length - httpHeaderData.Length];
                //Buffer.BlockCopy(httpData, httpHeaderData.Length, body, 0, body.Length);
                //Console.WriteLine("http内容如下"r"n{0}", Encoding.UTF8.GetString(body));
            }
        }
    }
}